package com.iwhalecloud.bss.kite.cucc.service.reception.bo;


import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.JsonUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQueryDevInfoService;
import com.iwhalecloud.bss.kite.cucc.client.dto.login.CuccLoginInfoDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CustAttrDTO;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.callclient.constant.CallClientEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("CustReceptionBO")
public class CustReceptionBO {

    @Autowired
    private ILocalQueryDevInfoService queryDevInfoService;

    public DataResult<CuccCustInfoDTO> getCustInfo(String key) {
        CuccCustInfoDTO custInfoDTO = this.queryCustInfo(key);
        //设置集团归属地
        if (custInfoDTO != null) {
        	String location = queryDevInfoService.queryAreaNameByCode(custInfoDTO.getEparchyProvince(), custInfoDTO.getEparchyCity());
            custInfoDTO.setLocation(location);
        }
        return  ErrorConsts.SUCCESS.getResult(custInfoDTO);
    }


    //调微服务获取客户信息报文
    public  CuccCustInfoDTO queryCustInfo(String key) {
        // 处理入参
    	// 处理入参
        Map<String, String> busiParams = new HashMap<>();
        busiParams.put("key", key);
        //调联通的接口获取客户信息
        Map resultMap = HttpsCallClient.get(CallClientEnum.QRY_CUCC_CUSTINFO, busiParams);
        Map dataMap = KiteMapUtils.getMap(resultMap, "data");
        if (dataMap != null) {
        	Map customerMap = KiteMapUtils.getMap(dataMap, "customer");
            CuccCustInfoDTO cuccCustInfoDTO = parseCustInfo(customerMap);
            return cuccCustInfoDTO;
        }
        return null;
    }

    //组装客户信息对象
    public static CuccCustInfoDTO parseCustInfo (Map result){
        CuccCustInfoDTO cuccUserInfoDTO = new CuccCustInfoDTO();
        cuccUserInfoDTO.setCbssId(result.get("cbssId").toString());
        cuccUserInfoDTO.setGroupId(result.get("groupId").toString());
        cuccUserInfoDTO.setCustId(result.get("custId").toString());
        cuccUserInfoDTO.setCustType(result.get("custType").toString());
        cuccUserInfoDTO.setName(result.get("name").toString());
        cuccUserInfoDTO.setCustName(result.get("name").toString());
        cuccUserInfoDTO.setEparchyProvince(result.get("eparchyProvince").toString());
        cuccUserInfoDTO.setEparchyCity(result.get("eparchyCity").toString());
        cuccUserInfoDTO.setEparchyArea(result.get("eparchyArea").toString());
        cuccUserInfoDTO.setContactName(result.get("contactName").toString());
        cuccUserInfoDTO.setContactPhone(result.get("contactPhone").toString());
        List<CustAttrDTO> custAttrDTO = JsonUtils.parseArray(result.get("attrs").toString(), CustAttrDTO.class);
        cuccUserInfoDTO.setAttrs(custAttrDTO);
        return cuccUserInfoDTO;
    }

    private static CuccCustInfoDTO getCustInfoDTO() {
        String s =
            "{\n" +
            "        \"address\": \"客户通讯地址客户通讯地址\",\n" +
            "        \"contactName\": \"测试\",\n" +
            "        \"bssId\": \"2019071132312696\",\n" +
            "        \"groupId\": \"5151051200210005763\",\n" +
            "        \"cbssId\": \"5119071116481925\",\n" +
            "        \"eparchyProvince\": \"51\",\n" +
            "        \"name\": \"订单中心测试组新建客户xy\",\n" +
            "        \"custType\": \"2\",\n" +
            "        \"custId\": \"5119071116481925\",\n" +
            "        \"eparchyCity\": \"510\",\n" +
            "        \"eparchyArea\": \"512002\",\n" +
            "        \"contactPhone\": \"13111111111\"\n" +
            "    }\n";

        CuccCustInfoDTO cuccCustInfoDTO = JSON.parseObject(s, CuccCustInfoDTO.class);
        return cuccCustInfoDTO;
    }



    public void ssoLogin(String sessionId) {
    	//HttpCallClient.post(centerCode, serviceCode, busiMap);
    	String result = "{\n" +
    			"	\"code\":\"0\",\n" +
    			"	\"message\":\"\",\n" +
    			"	\"data\":{\n" +
    			"		\"loginIp\":\"127.0.0.1\",\n" +
    			"		\"superAdminFlag\":true,\n" +
    			"		\"staffId\":\"1289335\",\n" +
    			"		\"roles\":\"超级管理员\",\n" +
    			"		\"roleTypes\":\"\",\n" +
    			"		\"ignoreBelong\":true,\n" +
    			"		\"userInfo\":{\n" +
    			"			\"userId\":100101,\n" +
    			"			\"userCode\":\"100101\",\n" +
    			"			\"userOrgId\":\"100101\",\n" +
    			"			\"userName\":\"邹测试\"\n" +
    			"		},\n" +
    			"		\"orgInfo\":{\n" +
    			"			\"orgId\":100101,\n" +
    			"			\"orgName\":\"佛山营业厅\",\n" +
    			"			\"orgCode\":\"100101\",\n" +
    			"			\"provinceCode\":\"52\",\n" +
    			"			\"eparchyCode\":\"521\",\n" +
    			"			\"areaCode\":\"521912\",\n" +
    			"			\"parentOrgId\":\"\",\n" +
    			"			\"orgLevel\":5\n" +
    			"		},\n" +
    			"		\"roleList\":[{\n" +
    			"			\"id\":18912101,\n" +
    			"			\"name\":\"超级管理员\",\n" +
    			"			\"code\":\"18912101\",\n" +
    			"			\"type\":\"\",\n" +
    			"			\"belong\":\"\",\n" +
    			"			\"belongName\":\"\"\n" +
    			"		}],\n" +
    			"		\"privInfos\":[{\n" +
    			"			\"privId\":1121,\n" +
    			"			\"sysOperId\":\"\",\n" +
    			"			\"privCode\":\"\",\n" +
    			"			\"privName\":\"\",\n" +
    			"			\"url\":\"\"\n" +
    			"		}]\n" +
    			"	}\n" +
    			"}";

    	//TODO 因为都使用springsession，使用相同的redis，这里直接从redis获取登录信息，不需要调接口
    	CuccLoginInfoDTO cuccLoginInfo = JSON.parseObject(result, CuccLoginInfoDTO.class);


    }

}
